﻿<%@ WebHandler Language="C#" Class="SearchPeopleHandler" %>

using System;
using System.Web;

public class SearchPeopleHandler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "application/json";
        string param = context.Request["q"];
        string ids = context.Request["ids"];
        string table = context.Request["tbl"];
        if (table == null)
            table = "";
        if (ids == null)
            ids = "";
        System.Collections.Generic.List<Iudicium.Persona.PersonaResultadoSimple> list = null;
        System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
        if (string.IsNullOrEmpty(param))
        {
            list = new System.Collections.Generic.List<Iudicium.Persona.PersonaResultadoSimple>();
            context.Response.Write(js.Serialize(list));
            return;
        }
        try
        {
            string where = " ";
            if (string.IsNullOrEmpty(param))
            {
                where = " 1 = 1 ";
                return;
            }
            else
            {
                where += Iudicium.Busqueda.UtilitatiosBusqueda.CrearQuery(Iudicium.Busqueda.UtilitatiosBusqueda.TipoDatoDb.TextoCompleto, "[nombre]", param) + " OR ";
                where += Iudicium.Busqueda.UtilitatiosBusqueda.CrearQuery(Iudicium.Busqueda.UtilitatiosBusqueda.TipoDatoDb.Texto, "[cedula]", param);
            }

            list = Iudicium.Persona.CRN.PersonaNaturalCRN.GetPersonaNaturalResultadoSimpleBySearch(where, ids, table);
        }
        catch (Exception ex)
        {
            log4net.ILog log = log4net.LogManager.GetLogger("LogFile");
            log.Error("No se pudo realizar la busqueda correctamente", ex);
            list = new System.Collections.Generic.List<Iudicium.Persona.PersonaResultadoSimple>();
        }
        string serializedList = "{}";
        try
        {
            serializedList = js.Serialize(list);
        }
        catch { }
        context.Response.Write(serializedList);
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}